import {
    createSlice,
} from "@reduxjs/toolkit";
import {
    deleteHospitalRemove, deleteHospitalSetBatchRemove,
    getHospitalSet,
    getHospitalSetLockId,
    postHospitalSetSave,
    putHospitalSet
} from "@/api/hosp";
import {useSelector} from "react-redux";
import {changeLoading, changePageInfo} from "@/store/slice/common";
import React from "react";

const hospSlice = createSlice({
    name:"hosp",
    initialState:{
        hospList:[],
        params:{}
    },
    reducers:{
        changeParams(state,{payload}){
          state.params = payload;
        },
        upHospList(state,{payload}){
            state.hospList = payload;
        },
        changeStateById(state,{payload}){
            const info:any = state.hospList.find((v:any)=>v.id === payload);
            info.state = info.state === 0?1:0;
        }
    }
})
export const {upHospList,changeParams} = hospSlice.actions
//获取列表
export const getHospitalSetAsync = function (page:number,limit:number,){
    return async (dispatch:any,gatState:any)=>{
        dispatch(changeLoading(true))
       const result =  await getHospitalSet(page,limit,gatState().hosp.params)
        // console.log(result)
        dispatch(upHospList(result.data.records))
        dispatch(changePageInfo({
            current:result.data.current,
            total:result.data.total,
            pageSize:result.data.size,
        }))
        dispatch(changeLoading(false))
    }
}

// 完成锁定2:更新服务端的锁定状态,然后重新获取当前页的信息,进行渲染
// 优点:可以将最后操作以后的最新数据进行呈现
export const getHospitalSetLockIdAsync = function (id:number,status:number){
    return async (dispatch:any,getState:any)=>{
        await  getHospitalSetLockId(id,status)
        const {current,pageSize} = getState().common.pageInfo
        dispatch(getHospitalSetAsync(current,pageSize))
    }
}

//完成锁定1:优点:减少ajax请求
// export  const getHospitalSetLockIdAsync = function (id:number,status:number){
//     return async (dispatch:any)=>{
//         await getHospitalSetLockId(id,status)
//         dispatch(changeStateById(id))
//     }
// }
//添加或修改医院设置
export const postHospitalSetSaveAsync = function (body:any){
     return async (dispatch:any,gatState:any)=>{
         if(body.id){
             //修改
             await putHospitalSet(body)
         }else {
             await postHospitalSetSave(body)
         }
         const {current,pageSize} = gatState().common.pageInfo
         dispatch(getHospitalSetAsync(current,pageSize))
     }
}

//删除医院设置
export  const deleteHospitalRemoveASync = function (id:any,){
    return async (dispatch:any,gatState:any)=>{
       await deleteHospitalRemove(id)
        const {current,pageSize} = gatState().common.pageInfo
        await dispatch(getHospitalSetAsync(current,pageSize))
    }
}

//批量删除
export const deleteHospitalSetBatchRemoveAsync = function (data:React.Key[]){
    return async (dispatch:any,gatState:any)=>{
        await deleteHospitalSetBatchRemove(data)
        const {current,pageSize} = gatState().common.pageInfo
        await dispatch(getHospitalSetAsync(current,pageSize))
    }
}




export const useSelectorHosp = ()=> useSelector((state:any)=>state.hosp)
export default hospSlice.reducer